package dataStructure;

import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;

public class ItemSet implements Iterable<String>{
	//Number of appearances so far
	public int counter;
	
	private SortedSet<String> set;
	
	public ItemSet()
	{
		set = new TreeSet<String>();
	}
	
	public boolean contains(String element)
	{
		return set.contains(element);
	}
	
	public boolean addItem(String item)
	{
		if (set.contains(item))
			return false;
		set.add(item);
		return true;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((set == null) ? 0 : set.hashCode());
		return result;
	}
	
	@Override
	public String toString(){
		return String.format("%d@%d@%s\n",set.size(), counter,toCsvString());
	}
	
	public String[] GetSingletons()
	{
		return (String[]) set.toArray();
	}
	
	public String toCsvString()
	{
		StringBuilder sb = new StringBuilder();
		boolean first = true;
		for (String item : set) {
			if (first)
			{
				first = false;
				sb.append(item);
			}
			else
				sb.append(", "+ item);
		}
		return sb.toString();
	}
	
	public String getFirst()
	{
		return set.first();
	}

	@Override
	public Iterator<String> iterator() {
		return set.iterator();
	}

	public String getLast() {
		return set.last();
	}
}
